package com.tjetc.view.teacherview;

import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Course;
import com.zhiyun.domain.CourseContent;
import com.zhiyun.domain.CourseUnit;
import com.zhiyun.service.CourseContentService;
import com.zhiyun.service.CourseUnitService;
import com.zhiyun.vo.CourseFile;
import com.zhiyun.vo.Result;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class MyCourseView_In_Content {
    CourseContentService courseContentService=CilentProxy.getProxyInstance(CourseContentService.class);
    CourseUnitService courseUnitService= CilentProxy.getProxyInstance(CourseUnitService.class);
    Scanner scanner=new Scanner(System.in);
    public void start(Course course){
        while (true){
            System.out.println("~~~~~~~~~~~课程 "+course.getName()+" 内容~~~~~~~~~~");
            System.out.println("1. 展示单元列表");
            System.out.println("2. 进入单元列表");
            System.out.println("3. 添加单元信息");
            System.out.println("4. 修改单元信息");
            System.out.println("5. 删除单元信息");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    allMulu(course);
                    break;
                case 2:
                    selectMuluById(course);
                    break;
                case 3:
                    inseretMulu(course);
                    break;
                case 4:
                    updateMulu(course);
                    break;
                case 5:
                    delMulu(course);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误,请重新输入");


            }
        }
    }

    private void inseretMulu(Course course) {
        while (true){
            System.out.println("请输入课程单元名称");
            String name = scanner.next();
            System.out.println("请输入课程描述");
            String describe = scanner.next();
            CourseUnit courseUnit = new CourseUnit(null, name, describe, course.getId());
            Result result = courseUnitService.insertCourseUnit(courseUnit);
            if (result.getCode()==200){
                System.out.println(result.getMsg());
            }else {
                System.out.println(result.getMsg());
            }
            break;
        }

    }

    private void updateMulu(Course course) {
        while (true){
            Result byCourseId = courseUnitService.findByCourseId(course.getId());
            if (byCourseId.getCode()==200){
                List<CourseUnit> obj = (List<CourseUnit>) byCourseId.getObj();
                for (CourseUnit courseUnit : obj) {
                    System.out.println("单元id"+courseUnit.getId()+"    单元名称:"+courseUnit.getName()+"    单元描述:"+courseUnit.getDecribe());
                }
                System.out.println("请输入想要修改的单元id");
                int i = scanner.nextInt();
                Result byCourseUnitId = courseContentService.findByCourseUnitId(i);
                if (byCourseUnitId.getCode()==200){
                    System.out.println("请输入修改后的单元名称");
                    String name = scanner.next();
                    System.out.println("请输入修改后的单元描述");
                    String describe = scanner.next();
                    CourseUnit courseUnit = new CourseUnit(i, name, describe, course.getId());
                    Result result = courseUnitService.updateCourseUnit(courseUnit);
                    if (result.getCode()==200){
                        System.out.println(result.getMsg());
                    }else {
                        System.out.println(result.getMsg());
                    }

                }else {
                    System.out.println(byCourseUnitId.getMsg());
                }
            }else {
                System.out.println(byCourseId.getMsg());

            }
            break;
        }
    }

    private void delMulu(Course course) {
        while (true){
            Result byCourseId = courseUnitService.findByCourseId(course.getId());
            if (byCourseId.getCode()==200){
                List<CourseUnit> obj = (List<CourseUnit>) byCourseId.getObj();
                for (CourseUnit courseUnit : obj) {
                    System.out.println("单元id"+courseUnit.getId()+"    单元名称:"+courseUnit.getName()+"    单元描述:"+courseUnit.getDecribe());
                }
                System.out.println("请输入想要删除的单元id");
                int i = scanner.nextInt();
                Result byCourseUnitId = courseContentService.findByCourseUnitId(i);
                if (byCourseUnitId.getCode()==200){
                    Result result = courseUnitService.deleteCourseUnit(i);
                    if (result.getCode()==200){
                        System.out.println(result.getMsg());
                    }else {
                        System.out.println(result.getMsg());
                    }
                }else {
                    System.out.println(byCourseUnitId.getMsg());
                }
            }else {
                System.out.println(byCourseId.getMsg());

            }
            break;
        }
    }

    private void selectMuluById(Course course) {
        while (true){
            Result byCourseId = courseUnitService.findByCourseId(course.getId());
            if (byCourseId.getCode()==200){
                List<CourseUnit> obj = (List<CourseUnit>) byCourseId.getObj();
                for (CourseUnit courseUnit : obj) {
                    System.out.println("单元id"+courseUnit.getId()+"    单元名称:"+courseUnit.getName()+"    单元描述:"+courseUnit.getDecribe());
                }
                System.out.println("请输入想要查看的单元");
                int i = scanner.nextInt();
                Result byCourseUnitId = courseContentService.findByCourseUnitId(i);
                if (byCourseUnitId.getCode()==200){
                   while (true){
                       System.out.println("1. 添加内容");
                       System.out.println("2. 读取内容");
                       System.out.println("3. 删除内容");
                       System.out.println("0. 返回上一级");
                       int i1 = scanner.nextInt();
                       switch (i1){
                           case 1:
                               try {
                                   addContent(byCourseUnitId,i);
                               } catch (IOException e) {
                                   e.printStackTrace();
                               }
                               break;
                           case 2:
                               selectContent(i);
                               break;
                           case 3:
                               delContent(i);
                               break;
                           case 0:
                               return;
                       }
                   }

                }
            }else {
                System.out.println("错误~~~~~~~~~~~~~~~~~~~~~~");

            }
            break;
        }
    }

    private void delContent( int i) {
       while (true){
           Result byCourseUnitId = courseContentService.findByCourseUnitId(i);
           List<CourseContent> obj1 = (List<CourseContent>) byCourseUnitId.getObj();
           for (CourseContent courseContent : obj1) {
               Result result = courseContentService.getCourseContentFile(courseContent.getId());
               if (result.getCode()==200){
                   CourseFile obj = (CourseFile) result.getObj();
                   System.out.println(courseContent.getId()+"    "  +courseContent.getName());
               }
           }
           System.out.println("请输入想要删除的内容id");
           int i1 = scanner.nextInt();
           Result result = courseContentService.deleteContent(i1);
           System.out.println(result.getMsg());
           break;
       }
    }

    private void addContent(Result byCourseUnitId, int i) throws IOException {
        System.out.println("请输入想要添加的本地文件目录");
        String src = scanner.next();
        File file = new File(src);
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] fileContent = new byte[(int) file.length()];
        int read = fileInputStream.read(fileContent);
        String fileName = src.substring(src.lastIndexOf("\\") + 1);
        System.out.println(fileName);
        CourseFile courseFile = new CourseFile(fileName, fileContent);
        CourseContent courseContent = new CourseContent();
        System.out.println("请输入内容名称");
        String name = scanner.next();
        courseContent.setName(name);
        courseContent.setResourceSrc(fileName);
        System.out.println("0是课件,1是视频,2是图片");
        int i1 = scanner.nextInt();
        courseContent.setCate(i1);
        courseContent.setUnitId(i);
        courseContent.setCreateTime(new Date());
        Result result = courseContentService.uploadCourseContent(courseContent, courseFile);
        System.out.println(result.getMsg());
    }

    private void selectContent(int i) {
       while (true){
           Result byCourseUnitId = courseContentService.findByCourseUnitId(i);
           List<CourseContent> obj1 = (List<CourseContent>) byCourseUnitId.getObj();
           for (CourseContent courseContent : obj1) {
               Result result = courseContentService.getCourseContentFile(courseContent.getId());
               if (result.getCode()==200){
                   System.out.println("id:"+courseContent.getId());
                   System.out.println("内容名称:"+ courseContent.getName());

               }
           }
           System.out.println("请输入想要查看的内容");
           int i1 = scanner.nextInt();
           Result result = courseContentService.getCourseContentFile(i1);
           if (result.getCode()==200){
               CourseFile obj2 = (CourseFile) result.getObj();
               System.out.println(obj2.getFileName());
               System.out.println(new String(obj2.getFileContent()));
           }
           break;
       }
    }

    private void allMulu(Course course) {
        while (true){
            Result byCourseId = courseUnitService.findByCourseId(course.getId());
            if (byCourseId.getCode()==200){
                List<CourseUnit> obj = (List<CourseUnit>) byCourseId.getObj();
                for (CourseUnit courseUnit : obj) {
                    System.out.println("单元id"+courseUnit.getId()+"    单元名称:"+courseUnit.getName()+"    单元描述:"+courseUnit.getDecribe());
                }
            }else {
                System.out.println("错误~~~~~~~~~~~~~~~~~~~~~~");
            }
            break;
        }
    }

}
